package com.ojoin.ojoin.eb_spider.service.catchdata.amazon;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.ojoin.ojoin.eb_spider.service.ISLDataCatchAtomStepService;
import com.ojoin.ojoin.eb_spider.service.SupplyInventoryService;
import com.ojoin.ojoin.eb_spider.service.WebDriverService;
import com.ojoin.trade.common.utils.NumberUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyInventory;


public abstract class AMAZONInstore implements
		ISLDataCatchAtomStepService<TbSlSupplyInventory> {
	public WebDriverService webDriver;
	public TbSlPlatformUser platformUser;
	private Logger log = LoggerFactory.getLogger(AMAZONInstore.class);
	@Autowired
	private SupplyInventoryService inventoryService;
	@Override
	public void sendKeys() {

	}

	@Override
	public void submit() {
	}

	@Override
	public void run() throws IOException, Exception {
		get();
		sendKeys();
		submit();

	}

	@Override
	public List<TbSlSupplyInventory> result() {
		List<TbSlSupplyInventory> purchases = handleElement2Inv();

		return purchases;
	}

	public void init(WebDriverService webDriver, TbSlPlatformUser platformUser) {
		this.webDriver = webDriver;

		this.platformUser = platformUser;

	}

	public long batchLoadRealInvCost(List<TbSlSupplyInventory> purchases) {
		long currTotalCost = 0;
		Iterator<TbSlSupplyInventory> it = purchases.iterator();
		while (it.hasNext()) {

			TbSlSupplyInventory tb = it.next();

			String url = "https://vendorcentral.amazon.cn/st/vendor/members/po-mgmt/order?orderId="
					+ tb.getItemId();

			webDriver.get(url);

			String displayTotalCost = webDriver
					.getText(
							"css",
							"#vssApplicationControls > div:nth-child(2) > table > tbody > tr:nth-child(5) > td:nth-child(4)");
			String normalTotalCost = displayTotalCost.replace("￥", "")
					.replaceAll(",", "").trim();
			long centUnit = NumberUtils.getLongAmount(normalTotalCost);

			tb.setTotalCost(centUnit);

		}
		return currTotalCost;
	}

	public List<TbSlSupplyInventory> handleElement2Inv() {
		List<TbSlSupplyInventory> purchases = new ArrayList<TbSlSupplyInventory>();

		String vssDisplayGrid = webDriver.findElement("css", ".vssDisplayGrid")
				.getAttribute("id");

		if (StringUtils.isNotEmpty(vssDisplayGrid)) {

			String gridInstanceId = vssDisplayGrid.split("_")[1];

			List<Map> obj = webDriver.execJavaScript("return window.vssData_"
					+ gridInstanceId);

			for (Map data : obj) {

				String totalCostStr = String.valueOf(data.get("totalCost"));
				long totalamout = NumberUtils.getLongAmount(totalCostStr);
				if (0 == totalamout) {
					continue;
				}

				String time = String.valueOf(data.get("orderDate"));
				
				time = TimeUtils.getCurDate(TimeUtils.parseDate(time));

				// 跳过已出对账单的
				String itemid = String.valueOf(data.get("externalId"));
				TbSlSupplyInventory inventory = new TbSlSupplyInventory();
				inventory.setPlatformAccountId(platformUser.getId());
				inventory.setItemId(itemid);
				inventory.setStoreDate(time);
				inventory.setItemStatus(TbSlSupplyInventory.IN);
				inventory.setItemKey(itemid);
				inventory.setTotalCost(totalamout);
				purchases.add(inventory);

			}
		}

		return purchases;
	}

	@Override
	public void fiter(List<TbSlSupplyInventory> list,
			Map<String, TbSlSupplyInventory> map) {

	     inventoryService.removeInventoryCommon(list, map);

		batchLoadRealInvCost(list);

	}

}
